//格式化时间
import { Notification, MessageBox } from 'element-ui';
export function formatDate(date, fmt) {
  const o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    S: date.getMilliseconds(), // 毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (const k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
    }
  }
  return fmt;
}

export function message(title = '提示', message = '未知异常', type = 'error') {
  Notification({
    title: title,
    message: message,
    type: type,
  });
}

export function calcTimeInterval(startTime, endTime) {
  let mss = endTime.getTime() - startTime.getTime();
  let days = parseInt(mss / (1000 * 60 * 60 * 24)).toFixed(0); // 得到天数
  let hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)).toFixed(0); // 得到小时
  let minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60)).toFixed(0); // 得到分钟数
  let seconds = (mss % (1000 * 60)) / 1000; // 得到秒数
  return (days == 0 ? '' : days + '天') + (hours == 0 ? '' : hours + ':') + minutes + ':' + seconds.toFixed(0); // 返回值
}

/**
 * @description 根据特定城市/区获取对应的省市区数组
 * @param {*} cityList 所有的省市区列表
 * @param {*} city 特定城市
 * @returns
 */
export const getCurrCityArrByCity = (cityList, city) => {
  if (!city || cityList.length === 0) {
    return undefined;
  }

  let currCityArr = undefined;
  for (let i = 0; i < cityList.length; i++) {
    const item = cityList[i];
    if (item.label === city) {
      currCityArr = [city];
      break;
    }

    if (item.children && item.children.length > 0) {
      for (let j = 0; j < item.children.length; j++) {
        const childItem = item.children[j];
        if (childItem.label === city) {
          currCityArr = [item.label, childItem.label];
          break;
        }

        if (childItem.children && childItem.children.length > 0) {
          for (let k = 0; k < childItem.children.length; k++) {
            const childChildItem = childItem.children[k];
            if (childChildItem.label === city) {
              currCityArr = [item.label, childItem.label, childChildItem.label];
              break;
            }
          }
        }
      }
    }
  }

  return currCityArr;
};

/**
 * @description 调价审核：商品的负毛利30%以上且比竞对最低价低5%以上--复审提示框
 * @param {*} cb
 */
export const priceAuditCheckMessageBox = cb => {
  MessageBox.alert(
    '本次审核如果商品的<span style="color: red">负毛利30%以上且比竞对最低价低5%以上</span>，会进入复审，你可在当前列表中查看审核结果~',
    '温馨提示',
    {
      dangerouslyUseHTMLString: true,
      confirmButtonText: '知道了',
      callback: action => {
        cb && cb(action);
      },
    },
  );
};
